package com.example.demo.service.impl;

import com.example.demo.Mapper.AdminMapper;
import com.example.demo.model.Admin;
import com.example.demo.model.AdminExample;
import com.example.demo.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;
    /*
    登入
     */
    @Override
    public Admin adminLogin(String name, String password) {
        AdminExample adminExample=new AdminExample();
        AdminExample.Criteria criteria=adminExample.createCriteria();
        criteria.andAdminNameEqualTo(name).andAdminPwdEqualTo(password);

        List<Admin> admins=adminMapper.selectByExample(adminExample);
        if(admins.size()==0||admins.get(0).getDeleteFlag().equals("1"))
        {
            return null;
        }
        return admins.get(0);
    }

    /**
     * 通过条件查询，其中各个参数可为空
     * @param id
     * @param name
     * @param phone
     * @param email
     * @return
     */
    @Override
    public List<Admin> selectByCondition(String id, String name, String phone, String email) {
        AdminExample adminExample=new AdminExample();
        AdminExample.Criteria criteria=adminExample.createCriteria();

        if (id!=null)
            criteria.andAdminIdEqualTo(Integer.parseInt(id));
        if(name!=null)
            criteria.andAdminNameEqualTo(name);
        if(phone!=null)
            criteria.andAdminPhoneEqualTo(phone);
        if(email!=null)
            criteria.andAdminEmailEqualTo(email);
        criteria.andDeleteFlagEqualTo("0");
        return adminMapper.selectByExample(adminExample);
    }

    @Override
    public List<Admin> selectByCondition(String id, String name, String phone, String email,String deleteFlag) {
        AdminExample adminExample=new AdminExample();
        AdminExample.Criteria criteria=adminExample.createCriteria();

        if (id!=null)
            criteria.andAdminIdEqualTo(Integer.parseInt(id));
        if(name!=null)
            criteria.andAdminNameEqualTo(name);
        if(phone!=null)
            criteria.andAdminPhoneEqualTo(phone);
        if(email!=null)
            criteria.andAdminEmailEqualTo(email);
        if (deleteFlag!=null)
            criteria.andDeleteFlagEqualTo(deleteFlag);

        return adminMapper.selectByExample(adminExample);
    }


    @Override
    @Transactional
    public int adminUpdate(Admin admin) {
        return adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    @Transactional
    public int adminDelete(Admin admin) {
        AdminExample adminExample=new AdminExample();
        AdminExample.Criteria criteria=adminExample.createCriteria();

        if (admin.getAdminId()!=null)
            criteria.andAdminIdEqualTo(admin.getAdminId());
        if(admin.getAdminName()!=null)
            criteria.andAdminNameEqualTo(admin.getAdminName());
        if(admin.getAdminPhone()!=null)
            criteria.andAdminPhoneEqualTo(admin.getAdminPhone());
        if(admin.getAdminEmail()!=null)
            criteria.andAdminEmailEqualTo(admin.getAdminEmail());

        admin.setDeleteFlag("1");
        return adminMapper.updateByExampleSelective(admin,adminExample);
    }

    @Override
    @Transactional
    public int adminInsert(Admin admin) {
        return adminMapper.insertSelective(admin);
    }
}
